﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MuaBanLike.com
{
    public partial class MainWindow : Window
    {
        private int m_nRetry = 0;

        private int m_nEarnedCoins = 0;
        private void startDetectStatusThread()
        {
            startTime = DateTime.Now;
            thread = new BackgroundWorker();
            thread.DoWork += delay2second;
            thread.RunWorkerCompleted += finishDetectResult;
            thread.WorkerReportsProgress = true;
            thread.WorkerSupportsCancellation = true;
            thread.RunWorkerAsync();
        }

        public bool m_bCanSkip = true;

        private void finishDetectResult(Object sender, RunWorkerCompletedEventArgs e)
        {
            var doc = (mshtml.HTMLDocument)MainBrowser.Document;

            mshtml.HTMLDivElement HintDiv = (mshtml.HTMLDivElement)doc.getElementById("Hint");

            if (HintDiv != null && HintDiv.innerHTML != null)
            {
                string sHtml = HintDiv.innerHTML;                
                if (sHtml.Contains("class=error"))
                {
                    if (m_bCanSkip)
                    {
                        onSkipLikeFaceBook();
                        thread.CancelAsync();
                        startDetectStatusThread();
                    }
                    else
                    {
                        thread.CancelAsync();
                        delayTimeToNext();
                    }
                }
                else if ((sHtml.Contains("SUCCESS") && sHtml.Contains("skipped")) || (sHtml.Contains("class=info") && sHtml.Contains("<DIV class=msg>")))
                {
                    thread.CancelAsync();
                    delayTimeToNext();
                }
                else if (sHtml.Contains("class=success"))
                {
                    m_nEarnedCoins += m_likeItem.Coins;
                    string msg = String.Format("Ban nhan duoc {0} coins", m_nEarnedCoins);
                    tbCoins.Text = msg;
                    thread.CancelAsync();
                    delayTimeToNext();
                }
                else // prevent page is loading not complete .
                {
                    //Still the page don't update status -> only remove 
                    if (m_nRetry < 8)
                    {
                        m_nRetry++;
                        startDetectStatusThread();
                    }
                    else
                    {
                        if (m_bCanSkip)
                        {
                            m_nRetry = 0;
                            onSkipLikeFaceBook();
                            thread.CancelAsync();
                            startDetectStatusThread();
                        }
                        else
                        {
                            thread.CancelAsync();
                            delayTimeToNext();
                        }
                    }
                }

            }
            else
            {

                thread.CancelAsync();
                delayTimeToNext();

            }

        }
    }
}
